﻿using Elastic.Clients.Elasticsearch;
using Elastic.Transport;

namespace CICDk8s
{
    public static class CustomServiceExtension
    {
        public static void AddElasticsearchClient(this IServiceCollection serviceCollection, ConfigurationManager configurationManager)
        {
            var esConfig = configurationManager.GetSection("EsConfig").Get<EsConfig>();

            var settings = new ElasticsearchClientSettings(esConfig.NodeUrls[0])
                //.CertificateFingerprint(esConfig.Fingerprint);
                .Authentication(new BasicAuthentication(esConfig.Username, esConfig.Password));

            serviceCollection.AddSingleton(new ElasticsearchClient(settings));
        }
    }
    public class EsConfig
    {
        // ES节点URI
        public Uri[] NodeUrls { get; set; } = null!;

        // CA 证书
        public string Fingerprint { get; set; } = null!;

        public string Username { get; set; } = null!;
        public string Password { get; set; } = null!;
    }
}
